package com.spring.activityApi.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spera.genutil.TokenUtil;
import com.spring.activityApi.web.dao.ActivityApiMapper;
import com.spring.activityApi.web.service.ActivityApiService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;

/**
 * Created by wojunyu on 2015/7/26.
 */
@Service
public class ActivityApiServiceImpl implements ActivityApiService {
    @Resource
    private ActivityApiMapper activityApiMapper;
    
	@Resource
	private JedisCache jedisCache;
//    @Resource
//    private RedisCache redisCache;

	/*@Override
	public List<ActivityList> getActivityList(String c_user_id,String flag) {
		// TODO Auto-generated method stub
		HashMap<String,Object> m=new HashMap<String,Object>();
		
		if(flag.equals("2")){
			m.put("c_user_id", c_user_id);
			m.put("flag", flag);
			return activityApiMapper.getMyActivityList(m);
		}
		if(flag.equals("0")||flag.equals("1")){
			
			m.put("flag", flag);
			return activityApiMapper.getActivityList(m);
		}
		System.out.println(m.toString());
		return null;
	}



	@Override
	public List<ActivityDetail> getActivityDetail(String c_user_id,String activity_info_id) {
		HashMap<String,Object> m=new HashMap<String,Object>();
		List<ActivityDetail> list=activityApiMapper.getActivityDetail(activity_info_id);
		m.put("c_user_id", c_user_id);
		m.put("activity_info_id", activity_info_id);
		String state=activityApiMapper.getActivityDetailState(m);
		System.out.println("state"+state);
		if(list.size()==1){
		list.get(0).setUser_state(state);
		}
		return list;
	}


	@Override
	public int attendActivity(String c_user_id, String activity_info_id) {
		HashMap<String,Object> hm = new HashMap<String,Object>();
	    hm.put("c_user_id", c_user_id);
	    hm.put("activity_info_id", activity_info_id);
	    hm.put("status", "");
	    activityApiMapper.attendActivity(hm);
	    System.out.println(hm.toString());
	    int a =  (int) hm.get("status");
	    System.out.println(a);
		return a;
	}
   */

	
	@Override
	public Map<String, Object> getActivityList(String activityID,String beginTime, String endTime, 
			String activityTypeCode, String name, String isAppliable, String pageSize, String pageIndex, 
			String skip, String rows) throws Exception {
		Map<String, Object> m = new HashMap();
		
		/*Map<String, Object> t = checkToken(token);
		if (t != null) {

			return t;

		}

		String c_user_id = redisCache.lindex(token, 0);*/
		
		
			/*
			HashMap hmc=MyTools.checkIsNotNull(rechargecard_id);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}
			*/
			
			HashMap hm=MyTools.pageIndexToSkip(pageSize, pageIndex, skip, rows);
			hm.put("activityID", activityID);
			hm.put("beginTime", beginTime);
			hm.put("endTime", endTime); 
			hm.put("activityTypeCode", activityTypeCode); 
			hm.put("name", name); 
			hm.put("isAppliable", isAppliable); 
			
			List<HashMap> list = activityApiMapper.getActivityList(hm);
			
			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}
	
	@Override
	public Map<String, Object> appointActivity(String token, String cUserName,String activityID) throws Exception {
		Map<String, Object> m = new HashMap();
		
		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String cUserID = jedisCache.hget(token, TokenUtil.USER_ID);
		//String cUserName = jedisCache.lindex(token, 3);
					
			HashMap hmc=MyTools.checkIsNotNull(cUserName,activityID);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}
			
			
			HashMap hm=new HashMap();
			hm.put("cUserID", cUserID);
			hm.put("cUserName", cUserName); 
			hm.put("activityID", activityID); 
			
			int count2=activityApiMapper.checkActivityID(hm);
			if(count2<1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请输入有效id");
				return m;
			}
			
			
     		int count = activityApiMapper.CheckDuplicationAppointment(hm);
			if(count>=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "请勿重复预约");
				return m;
			}
			
			int effect=activityApiMapper.appointActivity(hm);

			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "预约成功");

		
		return m;
	}

	@Override
	public Map<String, Object> cancelActivity(String token, String activity_appointmentID) throws Exception {
		Map<String, Object> m = new HashMap();
		
		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		String cUserID = jedisCache.hget(token, TokenUtil.USER_ID);
		//String cUserName = jedisCache.lindex(token, 3);
		
			HashMap hmc=MyTools.checkIsNotNull(activity_appointmentID);
			if(ApplicationUtils.isNotNull(hmc)){
				return hmc;
			}

			int count = activityApiMapper.cancelActivity(activity_appointmentID);
			if(count!=1){
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "取消失败");
				return m;
			}

			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "取消成功");

		
		return m;
	}
	
	
	@Override
	public Map<String, Object> getMyActivityList(String token, String activityTypeCode, String pageSize,
			String pageIndex, String skip, String rows) throws Exception {
		Map<String, Object> m = new HashMap();
		
		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

			String cUserID = jedisCache.hget(token, TokenUtil.USER_ID);
		//String cUserName = jedisCache.lindex(token, 3);
		
//			HashMap hmc=MyTools.checkIsNotNull(activityTypeCode);
//			if(ApplicationUtils.isNotNull(hmc)){
//				return hmc;
//			}
			HashMap hm=MyTools.pageIndexToSkip(pageSize, pageIndex, skip, rows);
			hm.put("cUserID", cUserID);

			List<HashMap> list = activityApiMapper.getMyActivityList(hm);

			m.put(ApplicationUtils.DATASET, list);
			m.put(ApplicationUtils.STATUS, "1");
			m.put(ApplicationUtils.MSG, "查询成功");

		
		return m;
	}
}
